using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;

namespace Fated.SaveEngine {
    static class JSONSerializer {
        public static string ToJSONString(Dictionary<string, object> input) {
            string output = "{ ";

            foreach (string key in input.Keys) {
                if (input[key].GetType() == typeof(string)) { //If a string, treat as string
                    output += "\"" + key + "\" : \"" + ((string)input[key]) + "\", ";
                } else {
                    IJSON temp;
                    try {       //If custom class, it should implement ToJSON
                        temp = (IJSON)input[key];
                    } catch {   //That leaves numbers
                        double number = double.Parse(input[key].ToString());
                        output += "\"" + key + "\" : " + number.ToString(CultureInfo.InvariantCulture) + ", ";
                        continue;
                    }
                    output += "\"" + key + "\" : " + ToJSONString(temp.ToJSON()) + ", ";
                }
            }
            return output.Substring(0, output.Length - 2) + " }";
        }
    }
}
